package cn.jjzd.info.controller;

import cn.jjzd.common.pojo.PageResult;
import cn.jjzd.info.response.ArticleResponse;
import cn.jjzd.info.response.ArticleListResponse;
import cn.jjzd.info.response.ArticlePageResponse;
import cn.jjzd.info.request.ArticleRequest;
import cn.jjzd.info.pojo.Article;
import cn.jjzd.info.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("article")
public class ArticleController {
    @Autowired
    private ArticleService sysArticleService;

    @GetMapping("page")
    public ResponseEntity<PageResult<ArticlePageResponse>> queryArticlePage(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "sortBy",required = false)String sortBy,
            @RequestParam(value = "desc",required = false)Boolean desc,
            @RequestParam(value = "cid",required = false)Long cid
    ){
        PageResult<ArticlePageResponse> result = sysArticleService.queryArticlePage(key,page,rows,sortBy,desc,cid);
        if(result == null || CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping
    public ResponseEntity<Void> saveInfo(@RequestBody Article articled){
        sysArticleService.saveArticle(articled);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateArticle(@RequestBody Article articled){
        sysArticleService.updateArticle(articled);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("list")
    public ResponseEntity<Map<String,List<ArticleResponse>>> queryArticleListByCids(@RequestParam("cids") List<Long> cids){
        Map<String,List<ArticleResponse>> map = new HashMap<>();
        List<ArticleResponse> articles = sysArticleService.queryArticBoList(cids);
        if((articles == null || CollectionUtils.isEmpty(articles))){
            return ResponseEntity.notFound().build();
        }
        map.put("infomation",articles);
        return ResponseEntity.ok(map);
    }

    @GetMapping("{id}")
    public ResponseEntity<Article> queryById(@PathVariable("id") Long id){
        Article sysArticle  = sysArticleService.queryById(id);
        if(sysArticle == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(sysArticle);
    }

    @PostMapping("morePage")
    public ResponseEntity<PageResult<ArticleListResponse>> queryArticlePage(@RequestBody ArticleRequest request){
        PageResult<ArticleListResponse> result = sysArticleService.queryArticleMorePage(request);
        if(result == null || CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("delete")
    public ResponseEntity<Void> deleteById(@RequestParam("id")Long id){
        sysArticleService.deleteById(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
